﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonLibrary.Common
{
    /// <summary>
    /// 名称：节点类
    /// 功能：用于显示节点树的关系
    /// 创建时间：2012-03-28
    /// 创建人：mlh
    /// 使用建议：使用的时候一般有一个空的节点作为根节点，在下面的才是真正的树
    /// </summary>
    public class Node
    {
        #region 构造器

        public Node()
        {
            IntityData("", "", false, false, false, "");
        }
        public Node(string text)
        {
            IntityData(text, "", false, false, false, "");
        }
        public Node(string text, string value)
        {
            IntityData(text, value, false, false, false, "");
        }
        public Node(string text, string value, bool expand)
        {
            IntityData(text, value, expand, false, false, "");
        }
        public Node(string text, string value, bool expand, bool showcheckbox)
        {
            IntityData(text, value, expand, showcheckbox, false, "");
        }
        public Node(string text, string value, bool expand, bool showcheckbox, bool ischecked)
        {
            IntityData(text, value, expand, showcheckbox, ischecked, "");
        }
        public Node(string text, string value, bool expand, bool showcheckbox, bool ischecked, string tag)
        {
            IntityData(text, value, expand, showcheckbox, ischecked, tag);
        }

        #endregion

        #region 参数

        /// <summary>
        /// default:  ""
        /// </summary>
        public string Text { set; get; }

        /// <summary>
        /// default：""
        /// </summary>
        public string Value { set; get; }

        /// <summary>
        /// 是否展开 
        /// default: false
        /// </summary>
        public bool Expand { set; get; }

        public string Tag { set; get; }

        /// <summary>
        /// 是否显示选择框 
        /// default: false
        /// </summary>
        public bool ShowCheckBox { set; get; }

        public bool IsChecked { set; get; }

        public List<Node> Nodes { set; get; }

        #endregion

        #region 方法

        void IntityData(string text, string value, bool expand, bool showcheckbox, bool ischecked, string tag)
        {
            Text = text;
            Value = value;
            Expand = expand;
            ShowCheckBox = showcheckbox;
            IsChecked = ischecked;
            Tag = tag;
            Nodes = new List<Node>();
        }

        public static string ConvertToZtree(Node tree)
        {
            //if (0 == tree.Nodes.Count)
            //    return "[]";
            //else
            //    return ZTreeHelper.Serialize(tree.Nodes);

            List<Node> nlist = new List<Node>();
            nlist.Add(tree);
            return ConvertToZtree(nlist);
        }

        public static string ConvertToZtree(List<Node> tree)
        {
            if (0 == tree.Count)
                return "[]";
            else
                return ZTreeHelper.Serialize(tree);
        }

        #endregion

        #region 子类

        class ZTreeHelper
        {
            /**  ztree 前台 json 格式
       
               var zTreeNodes = [{
               "id":1,
               "name":"我的好友",
               "open":true,
               "tag":"123",
               "nodes":[{
                              "id":11,
                              "name":"08001",
                              "open":true,
                              "tag":"123",
                           },{
                              "id":12,
                              "name":"08002",
                              "open":true,
                              "tag":"123",
                          },{
                             "id":13,
                             "name":"08003",
                             "open":true,
                             "tag":"123",
                         }]
                }];
         **/

            static string NodeString_NoChild = "  id:{0},name:'{1}',nocheck:{2},checked:{3},open:{4},tag:{5} ";
            static string NodeString_HaveChild = " id:{0},name:'{1}',nocheck:{2},checked:{3},open:{4},tag:{5},children:{6} ";
            public static string Serialize(List<Node> nodes)
            {
                if (null == nodes) return "";
                string result = "";
                //根节点循环
                for (int i = 0; i < nodes.Count; i++)
                {

                    //如果是叶节点
                    if (nodes[i].Nodes.Count == 0)
                    {
                        if (nodes.Count > 1) //如果该层有多个节点
                        {
                            if (i == 0)//如果是第一个节点
                            {
                                result += " [{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag)) + " } ";
                            }
                            else if (i == nodes.Count - 1)//如果是第最后一个节点
                            {
                                result += " ,{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag)) + " }] ";
                            }
                            else  //中间几点
                            {
                                result += " ,{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag)) + " } ";
                            }
                        }
                        else
                        {
                            result += " [{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag)) + " }] ";
                        }
                    }
                    //else if (nodes[i].ChildNodes.Count == 1)
                    else
                    {

                        if (nodes.Count > 1) //如果该层有多个节点
                        {
                            if (i == 0)//如果是第一个节点
                            {
                                result += " [{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag), Serialize(nodes[i].Nodes)) + " } ";
                            }
                            else if (i == nodes.Count - 1)//如果是第最后一个节点
                            {
                                result += " ,{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag), Serialize(nodes[i].Nodes)) + " }] ";
                            }
                            else  //中间几点
                            {
                                result += " ,{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag), Serialize(nodes[i].Nodes)) + " } ";
                            }
                        }
                        else
                        {
                            result += " [{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].IsChecked, nodes[i].Expand, escapeToJs(nodes[i].Tag), Serialize(nodes[i].Nodes)) + " }] ";
                        }
                    }
                    //else
                    //{
                    //    TreeNode[] childnodes = new TreeNode[nodes[i].ChildNodes.Count];
                    //    nodes[i].ChildNodes.CopyTo(childnodes, 0);
                    //    result += " [{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, Serialize(childnodes)) + " }] ";

                    //}
                }


                return result.ToLower();
            }


            public static String escapeToJs(String str)
            {
                if (str == null || str.Length == 0)
                {
                    return "''";
                }
                char b, c = default(char);
                int len = str.Length;
                StringBuilder sb = new StringBuilder(len + 4);
                String t = string.Empty;
                sb.Append("'");
                for (int i = 0; i < len; i++)
                {
                    b = c;
                    c = str.ToCharArray()[i];
                    switch (c)
                    {
                        case '\\':
                            sb.Append("\\\\");
                            break;
                        case '\'':
                            sb.Append("\\'");
                            break;
                        case '/':
                            if (b == '<') sb.Append("\\");
                            sb.Append(c);
                            break;
                        case '\b':
                            sb.Append("\\b");
                            break;
                        case '\t':
                            sb.Append("\\t");
                            break;
                        case '\n':
                            sb.Append("\\n");
                            break;
                        case '\f':
                            sb.Append("\\f");
                            break;
                        case '\r':
                            sb.Append("\\r");
                            break;
                        default:
                            sb.Append(c);
                            break;
                    }
                }
                sb.Append("'");
                try { return sb.ToString(); }
                finally { sb = null; }
            }
        }



        #endregion

    }


}
